/*
* © 2010 Autodata Solutions Company. All Rights Reserved
* This source code is the confidential and proprietary information of Autodata Solutions Company.
* The user shall not, in whole or in part, copy, publish, disclose or make any use of this source code
* without the prior written consent of Autodata Solutions Company.
*/
Ext.ns('net.autodata.compare.editorial');
(function() {

    /**
    * Display a editorial detail with a customizable view
    * @class EditorialDetailPanel
    * @extends Ext.BoxComponent
    * @namespace net.autodata.compare.editorial
    */
    var Panel = Ext.extend(Ext.BoxComponent, {

        autoEl: {
            tag: 'div',
            cls: 'editorial-detail-panel'
        },

        /**
        * @property detail
        * @type Object
        */
        detail: undefined,

        /**
        * @property view
        * @type Object
        */
        view: undefined,

        constructor: function() {
            this.addEvents({
                /**
                * @event click
                * Fires when this panel is clicked
                * @param {EventObject} e The click event
                * @param {Object} o The element that triggered the click event
                */
                'click': true
            });

            Panel.superclass.constructor.apply(this, arguments);
        },

        initComponent: function() {
            Panel.superclass.initComponent.apply(this, arguments);

            var view = this.getView();
            view.init(this);

            this.relayEvents(view, ['remove']);
        },

        /**
        * @private
        */
        initDetailEl: function(el) {
            this.mon(el, {
                scope: this,
                click: this.onClick
            });
        },

        /**
        * @method getView
        */
        getView: function() {
            if (!this.view) {
                this.view = new net.autodata.compare.widgets.EditorialDetailView(this.viewCfg);
            }

            return this.view;
        },

        /**
        * @method getDetail
        * @returns Object
        */
        getData: function() {
            return this.data;
        },

        /**
        * @method onClick
        * @private
        */
        onClick: function(e, o) {
            if (e) {
                e.preventDefault();
            }

            if (e.button !== 0) {
                return;
            }

            var el = Ext.get(o);

            if (el.hasClass('read-more')) {
                if (this.getData) {
                    var data = this.getData();
                    if (data.url) {
                        var form = ASC.Serializer.objectToForm({
                            action: data.url,
                            target: '_blank'
                        }, true);

                        form.submit();
                    }
                }
            }
        },

        /**
        * @method onRender
        * @private
        */
        onRender: function() {
            Panel.superclass.onRender.apply(this, arguments);

            this.initDetailEl(this.el);

            var view = this.getView();
            view.render();
        }
    });

    net.autodata.compare.editorial.EditorialDetailPanel = Panel;
})();
